package com.ymatou.iapi.liveseller.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.liveseller.parameter.SellerLiveOnlineBean;
import com.ymatou.iapi.liveseller.service.SellerLiveOnlineCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.utils.YMTDateUtil;
/**
*主播直播上线
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SellerLiveOnline{
	private static SellerLiveOnlineBean sellerliveonlineBean;
	private static SellerLiveOnlineCall sellerliveonlineCall;
	private static LiveRoomWapper liveRoomWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("主播直播上线");
	}

	@Before
	public void caseUp() {
		sellerliveonlineBean=new SellerLiveOnlineBean();
		sellerliveonlineCall=new SellerLiveOnlineCall();
		liveRoomWapper=new LiveRoomWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sellerliveonlineCall.getOpurl());
		liveRoomWapper=null;
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SellerLiveOnline_001() {
		Logger.start(true,"主播直播上线成功");
		try {
			CreateSellerLiveBean cb = LiveRoomCallService.initCreatesellerliveBean(null);
			cb.getLiveDto().setGroupId(LiveRoomCallService.CreateGroup("autotest",cb.getLiveDto().getUserId()));
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(cb);
			LiveRoomCallService.UpdateLiveTimeToStart(createlive.getId());
			sellerliveonlineBean.setLiveId(createlive.getId());
			sellerliveonlineBean.setUserId(createlive.getUserId());
			sellerliveonlineCall.setData(sellerliveonlineBean);
			sellerliveonlineCall.callService();			
			Logger.verifyEquals("200", sellerliveonlineCall.getString("Code"), "验证Code");
			LiveRoom dbliveroom = liveRoomWapper.selectLiveRoomById(createlive.getId());
			Logger.verifyEquals(2, dbliveroom.getLiveStatus(), "验证LiveStatus");
			Logger.verifyEquals(1, dbliveroom.getLivePush(), "验证LivePush");
			Logger.verifyNotNull(dbliveroom.getLivePushTime(), "验证第一次推流时间数据");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_SellerLiveOnline_002() {
		Logger.start(true,"不输入liveid失败");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			LiveRoomCallService.UpdateLiveTimeToStart(createlive.getId());
//			sellerliveonlineBean.setLiveId(createlive.getId());
			sellerliveonlineBean.setUserId(createlive.getUserId());
			sellerliveonlineCall.setData(sellerliveonlineBean);
			sellerliveonlineCall.callService();			
			Logger.verifyEquals("400", sellerliveonlineCall.getString("Code"), "验证Code");
			Logger.verifyEquals("直播Id不能为空", sellerliveonlineCall.getString("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_SellerLiveOnline_003() {
		Logger.start(true,"不输入sellerid失败");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			LiveRoomCallService.UpdateLiveTimeToStart(createlive.getId());
			sellerliveonlineBean.setLiveId(createlive.getId());
//			sellerliveonlineBean.setSellerId(createlive.getUserId());
			sellerliveonlineCall.setData(sellerliveonlineBean);
			sellerliveonlineCall.callService();			
			Logger.verifyEquals("400", sellerliveonlineCall.getString("Code"), "验证Code");
			Logger.verifyEquals("买手Id不能为空", sellerliveonlineCall.getString("Msg"), "验证Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SellerLiveOnline_004() {
		Logger.start(true,"输入sellerid和liveid不匹配不操作");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			LiveRoomCallService.UpdateLiveTimeToStart(createlive.getId());
			sellerliveonlineBean.setLiveId(createlive.getId());
			sellerliveonlineBean.setUserId(createlive.getUserId()+1);
			sellerliveonlineCall.setData(sellerliveonlineBean);
			sellerliveonlineCall.callService();			
			Logger.verifyEquals("201", sellerliveonlineCall.getString("Code"), "验证Code");
			Logger.verifyIsContains("不合法", sellerliveonlineCall.getString("Msg"), "验证Msg");
			LiveRoom dbliveroom = liveRoomWapper.selectLiveRoomById(createlive.getId());
			Logger.verifyEquals(0, dbliveroom.getLiveStatus(), "验证LiveStatus");
			Logger.verifyEquals(0, dbliveroom.getLivePush(), "验证LivePush");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SellerLiveOnline_005() {
		Logger.start(true,"主播重复上线");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			LiveRoomCallService.UpdateLiveTimeToStart(createlive.getId());
			sellerliveonlineBean.setLiveId(createlive.getId());
			sellerliveonlineBean.setUserId(createlive.getUserId());
			sellerliveonlineCall.setData(sellerliveonlineBean);
			sellerliveonlineCall.callService();		
			sellerliveonlineCall.callService();		
			Logger.verifyEquals("200", sellerliveonlineCall.getString("Code"), "验证Code");
			LiveRoom dbliveroom = liveRoomWapper.selectLiveRoomById(createlive.getId());
			Logger.verifyEquals(2, dbliveroom.getLiveStatus(), "验证LiveStatus");
			Logger.verifyEquals(1, dbliveroom.getLivePush(), "验证LivePush");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SellerLiveOnline_006() {
		Logger.start(true,"状态7系统自动关闭后调用上线失败");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			LiveRoomCallService.UpdateLiveTimeToStart(createlive.getId());
			liveRoomWapper.updateLiveRoomPushStatus(createlive.getId(), 7);
			sellerliveonlineBean.setLiveId(createlive.getId());
			sellerliveonlineBean.setUserId(createlive.getUserId());
			sellerliveonlineCall.setData(sellerliveonlineBean);
			sellerliveonlineCall.callService();		
			Logger.verifyEquals("201", sellerliveonlineCall.getString("Code"), "验证Code");
			Logger.verifyEquals("直播已关闭或已结束", sellerliveonlineCall.getString("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SellerLiveOnline_007() {
		Logger.start(true,"状态8未推流关闭直播调用上线失败");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			LiveRoomCallService.UpdateLiveTimeToStart(createlive.getId());
			liveRoomWapper.updateLiveRoomPushStatus(createlive.getId(), 8);
			sellerliveonlineBean.setLiveId(createlive.getId());
			sellerliveonlineBean.setUserId(createlive.getUserId());
			sellerliveonlineCall.setData(sellerliveonlineBean);
			sellerliveonlineCall.callService();		
			Logger.verifyEquals("201", sellerliveonlineCall.getString("Code"), "验证Code");
			Logger.verifyEquals("直播已关闭或已结束", sellerliveonlineCall.getString("Msg"), "验证Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SellerLiveOnline_008() {
		Logger.start(true,"状态9已推流结束直播调用上线失败");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			LiveRoomCallService.UpdateLiveTimeToStart(createlive.getId());
			liveRoomWapper.updateLiveRoomPushStatus(createlive.getId(), 9);
			sellerliveonlineBean.setLiveId(createlive.getId());
			sellerliveonlineBean.setUserId(createlive.getUserId());
			sellerliveonlineCall.setData(sellerliveonlineBean);
			sellerliveonlineCall.callService();		
			Logger.verifyEquals("201", sellerliveonlineCall.getString("Code"), "验证Code");
			Logger.verifyEquals("直播已关闭或已结束", sellerliveonlineCall.getString("Msg"), "验证Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SellerLiveOnline_009() {
		Logger.start(true,"状态10运营强制关停调用上线失败");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			LiveRoomCallService.UpdateLiveTimeToStart(createlive.getId());
			liveRoomWapper.updateLiveRoomPushStatus(createlive.getId(), 10);
			sellerliveonlineBean.setLiveId(createlive.getId());
			sellerliveonlineBean.setUserId(createlive.getUserId());
			sellerliveonlineCall.setData(sellerliveonlineBean);
			sellerliveonlineCall.callService();		
			Logger.verifyEquals("201", sellerliveonlineCall.getString("Code"), "验证Code");
			Logger.verifyEquals("直播已关闭或已结束", sellerliveonlineCall.getString("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SellerLiveOnline_010() {
		Logger.start(false,"时间未到主播直播上线失败");
		try {
			CreateSellerLiveBean cb = LiveRoomCallService.initCreatesellerliveBean(null);
			cb.getLiveDto().setGroupId(LiveRoomCallService.CreateGroup("autotest",cb.getLiveDto().getUserId()));
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(cb);
//			LiveRoomCallService.UpdateLiveTimeToStart(createlive.getId());
			sellerliveonlineBean.setLiveId(createlive.getId());
			sellerliveonlineBean.setUserId(createlive.getUserId());
			sellerliveonlineCall.setData(sellerliveonlineBean);
			sellerliveonlineCall.callService();			
			Logger.verifyEquals("201", sellerliveonlineCall.getString("Code"), "验证Code");
			Logger.verifyIsContains("还未到直播开始时间", sellerliveonlineCall.getString("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SellerLiveOnline_011() {
		Logger.start(true,"主播直播上线成功,有人订阅");
		try {
			CreateSellerLiveBean cb = LiveRoomCallService.initCreatesellerliveBean(null);
			cb.getLiveDto().setGroupId(LiveRoomCallService.CreateGroup("autotest",cb.getLiveDto().getUserId()));
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(cb);
			LiveRoomCallService.SubscribeLive(createlive.getId(), 1);
			LiveRoomCallService.UpdateLiveTimeToStart(createlive.getId());
			
			sellerliveonlineBean.setLiveId(createlive.getId());
			sellerliveonlineBean.setUserId(createlive.getUserId());
			sellerliveonlineCall.setData(sellerliveonlineBean);
			sellerliveonlineCall.callService();			
			Logger.verifyEquals("200", sellerliveonlineCall.getString("Code"), "验证Code");
			LiveRoom dbliveroom = liveRoomWapper.selectLiveRoomById(createlive.getId());
			Logger.verifyEquals(2, dbliveroom.getLiveStatus(), "验证LiveStatus");
			Logger.verifyEquals(1, dbliveroom.getLivePush(), "验证LivePush");
			Logger.verifyNotNull(dbliveroom.getLivePushTime(), "验证第一次推流时间数据");
			//TODO 验证日志 executing request: POST http://notify.iapi.ymatou.com/api/Message/SendNotify HTTP/1.1	request body: 
			//新增字段 title，pic ，mType，Content 
			//"AppId":"liveseller.iapi.ymatou.com","EndTime":1529724920112,"IsComment":false,"IsPushNotify":true,"Message":"{\"Content\":\"预约的「直播标题458412489920848283」主播喊你来围观~\",\"PKey\":16,\"PVal\":\"7824\",\"mType\":8,\"pic\":\"http://img.ymatou.com/app/flag/circle/America.png\",\"title\":\"直播即将开始\"}","NotifySenderUserId":30250723,"PushTitle":"预约的「直播标题458412489920848283」主播喊你来围观~","SkipSaveToDb":true,"ToUserId":1}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}